package com.example.demo3.controller;

import com.alibaba.fastjson.JSONArray;
import com.dsbj.common.msg.DataPageMessage;
import com.example.demo3.model.Menu;
import com.example.demo3.model.Student;
import com.example.demo3.model.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sys")
public class TestController extends BaseController{


    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/index")
    public String testThymeleaf(HttpServletRequest request, ModelMap modelMap){

        logger.debug("进入了首页...");
        List<Menu> menuList = new ArrayList<>();
        Menu menu1 = new Menu();
        menu1.setId("1");
        menu1.setName("购置管理");
        menu1.setParentId("");
        menu1.setSortNo("0");
        menu1.setIcon("iconfont icon-purchase");

        Menu menu2 = new Menu();
        menu2.setId("2");
        menu2.setName("采购计划");
        menu2.setParentId("1");
        menu2.setImgUrl("imgs/bgc/1.png");
        menu2.setDataUrl("/test");

        Menu menu3 = new Menu();
        menu3.setId("3");
        menu3.setName("采购申请");
        menu3.setParentId("1");
        menu3.setImgUrl("imgs/bgc/2.png");
        menu3.setDataUrl("/test");

        menu1.getSubMenus().add(menu2);
        menu1.getSubMenus().add(menu3);

        Menu menu4 = new Menu();
        menu4.setId("4");
        menu4.setName("设备管理");
        menu4.setParentId("");
        menu4.setSortNo("1");
        menu4.setIcon("iconfont icon-menu-device");

        Menu menu5 = new Menu();
        menu5.setId("5");
        menu5.setName("测试表格");
        menu5.setParentId("4");
        menu5.setImgUrl("imgs/bgc/3.png");
        menu5.setDataUrl("/test");

        Menu menu6 = new Menu();
        menu6.setId("6");
        menu6.setName("资产档案");
        menu6.setParentId("4");
        menu6.setImgUrl("imgs/bgc/4.png");
        menu6.setDataUrl("/resourceDevice/list");

        Menu menu7 = new Menu();
        menu7.setId("7");
        menu7.setName("jqgrid");
        menu7.setParentId("4");
        menu7.setImgUrl("imgs/bgc/4.png");
        menu7.setDataUrl("/resourceDeviceTwo/list");

        Menu menu8 = new Menu();
        menu8.setId("8");
        menu8.setName("设备修改");
        menu8.setParentId("4");
        menu8.setImgUrl("imgs/bgc/4.png");
        menu8.setDataUrl("/devicewcm/deviceHtml");
    
        Menu menu9 = new Menu();
        menu9.setId("9");
        menu9.setName("采购计划-测试");
        menu9.setParentId("4");
        menu9.setImgUrl("imgs/bgc/4.png");
        menu9.setDataUrl("/purPlan/list");
        
        Menu menu10 = new Menu();
        menu10.setId("10");
        menu10.setName("备件档案");
        menu10.setParentId("4");
        menu10.setImgUrl("imgs/bgc/4.png");
        menu10.setDataUrl("/spareParts/list");

        Menu menu11 = new Menu();
        menu11.setId("11");
        menu11.setName("故障码");
        menu11.setParentId("4");
        menu11.setImgUrl("imgs/bgc/4.png");
        menu11.setDataUrl("/codeDtc/list");
        Menu menu12 = new Menu();
        menu12.setId("12");
        menu12.setName("行动码");
        menu12.setParentId("4");
        menu12.setImgUrl("imgs/bgc/4.png");
        menu12.setDataUrl("/codeAction/list");
        Menu menu13 = new Menu();
        menu13.setId("13");
        menu13.setName("原因码");
        menu13.setParentId("4");
        menu13.setImgUrl("imgs/bgc/4.png");
        menu13.setDataUrl("/codeReason/list");
        Menu menu14 = new Menu();
        menu14.setId("14");
        menu14.setName("工具");
        menu14.setParentId("4");
        menu14.setImgUrl("imgs/bgc/4.png");
        menu14.setDataUrl("/tool/list");
        Menu menu15 = new Menu();
        menu15.setId("15");
        menu15.setName("危险源及措施");
        menu15.setParentId("4");
        menu15.setImgUrl("imgs/bgc/4.png");
        menu15.setDataUrl("/hazard/list");

        Menu menu20 = new Menu();
        menu20.setId("20");
        menu20.setName("Form查询表头");
        menu20.setParentId("4");
        menu20.setImgUrl("imgs/bgc/4.png");
        menu20.setDataUrl("/sys/formSearch");

        Menu menu21 = new Menu();
        menu21.setId("21");
        menu21.setName("字典数据");
        menu21.setParentId("4");
        menu21.setImgUrl("imgs/bgc/4.png");
        menu21.setDataUrl("/dict/list");

        Menu menu22 = new Menu();
        menu22.setId("22");
        menu22.setName("ztree");
        menu22.setParentId("4");
        menu22.setImgUrl("imgs/bgc/4.png");
        menu22.setDataUrl("/dict/ztreelist");

        menu4.getSubMenus().add(menu5);
        menu4.getSubMenus().add(menu6);
        menu4.getSubMenus().add(menu7);
        menu4.getSubMenus().add(menu8);
        menu4.getSubMenus().add(menu9);
        menu4.getSubMenus().add(menu10);
        menu4.getSubMenus().add(menu11);
        menu4.getSubMenus().add(menu12);
        menu4.getSubMenus().add(menu13);
        menu4.getSubMenus().add(menu14);
        menu4.getSubMenus().add(menu15);
        menu4.getSubMenus().add(menu20);
        menu4.getSubMenus().add(menu21);
        //menu4.getSubMenus().add(menu22);

        menuList.add(menu1);
        menuList.add(menu4);


        /**
         * 增加menuTest_cjw
         */
        Menu menuTest_cjw = new Menu();
        menuTest_cjw.setId("5");
        menuTest_cjw.setName("页面测试cjw");
        menuTest_cjw.setParentId("");
        menuTest_cjw.setSortNo("5");
        menuTest_cjw.setIcon("iconfont icon-menu-device");
        
        Menu menuDtable_1 = new Menu();
        menuDtable_1.setId("51");
        menuDtable_1.setName("dtable_1");
        menuDtable_1.setParentId("5");
        menuDtable_1.setImgUrl("imgs/bgc/4.png");
        menuDtable_1.setDataUrl("/mtest_cjw/dtable_1");
        
        menuTest_cjw.getSubMenus().add(menuDtable_1);
        menuList.add(menuTest_cjw);




        //  model.addAttribute("menuList",menuList);
        request.setAttribute("menuList",menuList);
        //modelMap.addAttribute("menuList", menuList);
        //modelMap.addAttribute("msg", "Hello dalaoyang , this is thymeleaf");
        return "index";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/formSearch")
    public String formSearch(){
        return "formSearch";
    }


    @RequestMapping("/mockDatas")
    @ResponseBody
    public DataPageMessage mockDatas(int pageindex,int pagesize,int sEcho){

        List<Student> list = new ArrayList<>();
        list.add(new Student("S001",18,1,"席胜远"));
        list.add(new Student("S002",19,0,"吴成美"));
        list.add(new Student("S003",20,1,"陈佳伟"));
        list.add(new Student("S004",22,1,"张键"));
        list.add(new Student("S005",18,1,"席胜远"));
        list.add(new Student("S006",19,0,"吴成美"));
        list.add(new Student("S007",20,1,"陈佳伟"));
        list.add(new Student("S008",22,1,"张键"));
        list.add(new Student("S009",18,1,"席胜远"));
        list.add(new Student("S010",19,0,"吴成美"));
        list.add(new Student("S011",20,1,"陈佳伟"));
        list.add(new Student("S012",22,1,"张键"));
        list.add(new Student("S013",18,1,"席胜远"));
        list.add(new Student("S014",19,0,"吴成美"));
        list.add(new Student("S015",20,1,"陈佳伟"));
        list.add(new Student("S016",22,1,"张键"));
        list.add(new Student("S017",18,1,"席胜远"));
        list.add(new Student("S018",19,0,"吴成美"));
        list.add(new Student("S019",20,1,"陈佳伟"));
        list.add(new Student("S020",22,1,"张键"));

        return DataPageMessage.ok(list.subList((pageindex-1)*pagesize,Math.min(pageindex*pagesize,list.size())),list.size(),list.size(),sEcho,"");
    }

    @RequestMapping("/wcm")
    public String wcm(HttpServletRequest request){
        List<TreeNode> trees = new ArrayList<>();
        TreeNode tree1 = new TreeNode();
        tree1.setId("1");
        tree1.setPId("0");
        tree1.setName("苹果");
        tree1.setOpen(false);

        TreeNode tree2 = new TreeNode();
        tree2.setId("2");
        tree2.setPId("0");
        tree2.setName("香蕉");
        tree2.setOpen(false);

        TreeNode tree3 = new TreeNode();
        tree3.setId("3");
        tree3.setPId("0");
        tree3.setName("橙子");
        tree3.setOpen(false);

        TreeNode tree4 = new TreeNode();
        tree4.setId("4");
        tree4.setPId("5");
        tree4.setName("圣女果");
        tree4.setOpen(false);

        TreeNode tree5 = new TreeNode();
        tree5.setId("5");
        tree5.setPId("0");
        tree5.setName("蔬菜");
        tree5.setOpen(true);

        trees.add(tree1);
        trees.add(tree2);
        trees.add(tree3);
        trees.add(tree4);
        trees.add(tree5);

        String json = JSONArray.toJSONString(trees);
        request.setAttribute("trees", json);

        return "wcm";
    }

}
